/**  192.168.0.155
 * Created by admin on 2017/9/12.
 */

define([], function () {
    var uri = {};
    if(webix.runningEnv == "production"){
        uri.imgUrl = "http://hdb2a.55tjr.com";
        uri.imgUploadUrl = "http://hdb2a.55tjr.com";
        uri.logUrl = "http://hdb2a.55tjr.com";
        uri.apiUrl = "http://hdb2a.55tjr.com";
        uri.jobUrl = "http://hdb2a.55tjr.com";
    }else if(webix.runningEnv == "ali-test"){
        uri.imgUrl = "http://admin.test.wd310.com";
        uri.imgUploadUrl = "http://admin.test.wd310.com";
        uri.logUrl = "http://admin.test.wd310.com";
        uri.apiUrl = "http://admin.test.wd310.com";
        uri.jobUrl = "http://admin.test.wd310.com";
    }else if(webix.runningEnv == "local-test"){
        uri.imgUrl = "http://194-admin.ngrok.wd310.com";
        uri.imgUploadUrl = "http://194-admin.ngrok.wd310.com";
        uri.logUrl = "http://192.168.0.194:18910";
        uri.apiUrl = "http://194-admin.ngrok.wd310.com";
        uri.jobUrl = "http://192.168.0.194:18886";
    }else if(!webix.runningEnv || webix.runningEnv == "development"){
        uri.imgUrl = "http://hdb2a.55tjr.com";
        uri.imgUploadUrl = "http://hdb2a.55tjr.com";
        uri.logUrl = "http://hdb2a.55tjr.com";
        uri.apiUrl = "http://hdb2a.55tjr.com";
        uri.jobUrl = "http://hdb2a.55tjr.com";
        // uri.imgUrl = "http://192.168.0.194:50889";
        // uri.imgUploadUrl = "http://192.168.0.194:50889";
        // uri.logUrl = "http://192.168.0.194:18910";
        // uri.apiUrl = "http://192.168.0.194:50889";
        // uri.jobUrl = "http://192.168.0.194:18886";
    }

    var commonAjaxPost = function (url, data, success, fail) {
        var xmlHttpReq = null;  //声明一个空对象用来装入XMLHttpRequest
        if (window.ActiveXObject) {//IE5 IE6是以ActiveXObject的方式引入XMLHttpRequest的
            xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else if (window.XMLHttpRequest) {//除IE5 IE6 以外的浏览器XMLHttpRequest是window的子对象
            xmlHttpReq = new XMLHttpRequest();//实例化一个XMLHttpRequest
        }
        xmlHttpReq.open("POST", url, true);
        if (data && data.username) {
            data = (function (obj) { // 转成post需要的字符串.
                var str = "";

                for (var prop in obj) {
                    str += prop + "=" + obj[prop] + "&"
                }
                return str;
            })(data);
            xmlHttpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xmlHttpReq.send(data);
        } else {
            xmlHttpReq.setRequestHeader("Content-Type", "application/json");
            xmlHttpReq.send(JSON.stringify(data));
        }
        xmlHttpReq.onreadystatechange = function () {
            (function () {
                try {
                    if (xmlHttpReq.status !== 200 && xmlHttpReq.status !== 304) {
                        webix.message({type: "error", text: "http status:" + xmlHttpReq.status});
                        return
                    }
                    if (xmlHttpReq.readyState === 4 && (xmlHttpReq.status === 200 || xmlHttpReq.status === 304)) {  // 304未修改
                        var repsonse = JSON.parse(xmlHttpReq.responseText);
                        if (repsonse.message === "未登录") {
                            // sessionStorage.removeItem("username");
                            // window.location.href = '/';
                            return
                        }
                        if (repsonse.exception) {
                            webix.message({type: "error", text: repsonse.exception + "  " + repsonse.message});
                            return
                        }
                        success && success(repsonse);
                    }
                }
                finally {
                    // webix.delay(function () {
                    //     if ($$("table") && $$("table").hideProgress) {
                    //         $$("table").hideProgress()
                    //     }
                    // }, this, {}, 300);
                }
            })()
        };
    };

    function ajax(url, data, success, fail) {
        commonAjaxPost(uri.apiUrl + url, data, success, fail);
    }

    function queryLogisticInfo(url, data, success, fail) {
        commonAjaxPost(uri.kuaidi100 + url, data, success, fail);
    }

    function ajaxLog(url, data, success, fail) {
        commonAjaxPost(uri.logUrl + url, data, success, fail);
    }

    function ajaxJob(url, data, success, fail) {
        commonAjaxPost(uri.jobUrl + url, data, success, fail);
    }

    return {
        url: uri,
        ajax: ajax,
        ajaxLog:ajaxLog,
        ajaxJob:ajaxJob,
        queryLogisticInfo: queryLogisticInfo
    }
})
